﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
namespace UTIL
{
    /// <summary>
    /// 修改配置文件类
    /// </summary>
    public class CConfigXml:IDisposable
    {
       #region 私有属性
        //操作的配置文档
        private  XmlDocument xd = null;
        private bool disposed = false;
        #endregion
        public  CConfigXml()
        {

        }
       #region 析构函数    
        ~CConfigXml()
        {
            Dispose(false);
        }
        #endregion

       #region 加载配置文件
        /// <summary>
       /// 加载配置文件
       /// </summary>
       /// <param name="strStartPath">配置文件路径</param>
       /// <returns></returns>
        public bool  LoadFile(String strStartPath)
        {
            //此处配置文件在程序目录下          
            try
            {               
                xd = new XmlDocument();
                xd.Load(strStartPath);                
                return true; 
            }
            catch (Exception me)
            {
                //ce.SaveExcetpion(e);
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                return false;
            }

        }        
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="strStartPath"></param>
        /// <returns></returns>
        public bool LoadFile()
        {
            //此处配置文件在程序目录下

            try
            {
                xd = new XmlDocument();
                xd.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                return true;
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
                return false;
            }

        }
        #endregion              

       #region 修改appSettings下的值
        /// <summary>
        /// 键名 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="?"></param>
        /// <returns></returns>
       public bool SetAppSettings(String key,String value)
       {
           try
           {
               XmlElement xe = xd.DocumentElement.SelectSingleNode("//appSettings/add[@key='"+key+"']") as XmlElement;
               if (xe != null)
               {
                   //xe.InnerText = value;
                   xe.SetAttribute("value", value);
               }
               return true; 
           }
           catch (Exception me)
           {
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
               return false;
           }
           
       }
        #endregion

       #region 获取appSettings的值
       public String  GetAppSettings(String key)
       {
           String strResult=String.Empty;
           try
           {
               XmlElement xe = xd.DocumentElement.SelectSingleNode("//appSettings/add[@key='" + key + "']") as XmlElement;
               if (xe != null)
               {
                   //xe.InnerText = value;
                   strResult = xe.GetAttribute("value");
               }
               return strResult;
           }
           catch (Exception me)
           {
               //ce.SaveExcetpion(e);
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
               strResult="";
               return strResult;
           }

       }
       #endregion

       #region 保存配置文件 
       public bool SaveFile(String strStartPath)
       {
            try
            {              
                if (xd != null)
                {
                    xd.Save(strStartPath);
                }
                return true; 
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
                xd = null;
                return false;
            }
       }

       public bool SaveFile()
       {
           try
           {

               if (xd != null)
               {
                   xd.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
               }
               return true;
           }
           catch (Exception me)
           {
               //ce.SaveExcetpion(e);
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
               xd = null;
               return false;
           }
       }
        #endregion

       #region 获取appSettings的值 A-2009-05 D-2010-04-20
       //public String GetAppSettings(String key,Boolean bAutoOpen)
       //{
       //    String strResult = String.Empty;           
       //    try
       //    {
       //        if (bAutoOpen)
       //        {
       //            if (!LoadFile())
       //            {
       //                return "";
       //            }
       //        }

       //        XmlElement xe = xd.DocumentElement.SelectSingleNode("//appSettings/add[@key='" + key + "']") as XmlElement;
       //        if (xe != null)
       //        {
       //            //xe.InnerText = value;
       //            strResult = xe.GetAttribute("value");
       //        }
       //        return strResult;
       //    }
       //    catch (Exception me)
       //    {
       //        //ce.SaveExcetpion(e);
       //        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
       //        strResult = "";
       //        return strResult;
       //    }

       //}
       #endregion

       #region 修改appSettings下的值 A-2009-05
       /// <summary>
       /// 键名 
       /// </summary>
       /// <param name="key"></param>
       /// <param name="?"></param>
       /// <returns></returns>
       public bool SetAppSettings(String key, String value, Boolean bAutoOpen)
       {
           try
           {
               if (bAutoOpen)
               {
                   if (!LoadFile())
                   {
                       return false;
                   }
               }

               XmlElement xe = xd.DocumentElement.SelectSingleNode("//appSettings/add[@key='" + key + "']") as XmlElement;
               if (xe != null)
               {
                   //xe.InnerText = value;
                   xe.SetAttribute("value", value);
               }
               return true;
           }
           catch (Exception me)
           {
               //ce.SaveExcetpion(e);
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
               return false;
           }

       }
       #endregion

       #region 释放资源
       public void Dispose()
       {
           Dispose(true);
           GC.SuppressFinalize(this);           
       }
       private void Dispose(bool disposing)
       {
           if (!this.disposed)
           {
               if (disposing)
               {
                
                   xd = null;
               }           
               disposed = true;
           }
       }
       #endregion       
    }
}
